package tu.j2ee.tasktracker.cron;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import tu.j2ee.tasktracker.dao.CommentDAO;
import tu.j2ee.tasktracker.dao.TaskChangeDAO;
import tu.j2ee.tasktracker.domain.Comment;
import tu.j2ee.tasktracker.domain.TaskChange;

@Service
public class MailCron {
	
	private final static String APP_PATH = "http://localhost:8080/TaskTracker";
	private final static String NEW_LINE = "\r\n";

	@Autowired
	TaskChangeDAO taskChangeDAO;
	
	@Autowired
	CommentDAO commentDao;
	
	@Autowired
	private MailSender mailSender;
	
	@Scheduled(fixedRate = 30000)//(cron = "0 0 1 * * *")//every day at 1 o'clock midnight
	public void sendMailTaskChange() {
		List<TaskChange> all = taskChangeDAO.findAll();
		SimpleDateFormat taskChangeFormat = new SimpleDateFormat("yyyy.MM.dd 'at' HH:mm:ss z");
		SimpleDateFormat estimateFormat = new SimpleDateFormat("yyyy.MM.dd");
		for (TaskChange taskChange : all) {
			
			SimpleMailMessage message = new SimpleMailMessage();
			message.setTo(taskChange.getAssignedUser().getEmail());
			
            message.setSubject("Your task has been changed " + 
            		taskChangeFormat.format(taskChange.getChangeDate()));
           
            StringBuilder str = new StringBuilder();
            str.append(APP_PATH + "/tasks/" + taskChange.getTask().getId()).append(NEW_LINE);

            str.append("Title: ").append(taskChange.getTitle()).append(NEW_LINE);
            str.append("Description: ").append(taskChange.getDescription()).append(NEW_LINE);
            str.append("Estimated date: ").append(estimateFormat.format(taskChange.getEstimatedDate()))
            	.append(NEW_LINE);
            str.append("Status: ").append(taskChange.getStatus()).append(NEW_LINE);
            message.setText(str.toString());
            mailSender.send(message);
            taskChangeDAO.delete(taskChange);
		}
	}
	
	@Scheduled(fixedRate = 30000)//(cron = "0 0 1 * * *")//every day at 1 o'clock midnight
	public void sendMailNewComment() {
		List<Comment> all = commentDao.findByMailSent(false);
		for (Comment comment : all) {
			SimpleMailMessage message = new SimpleMailMessage();
			message.setTo(comment.getTask().getAssignedUser().getEmail());
			message.setSubject("New comment");
			
			StringBuilder str = new StringBuilder();
			str.append(comment.getCustomUser().getUsername()).append(" commented on your task").append(NEW_LINE);
			str.append(APP_PATH + "/tasks/" + comment.getTask().getId()).append(NEW_LINE);
			str.append(comment.getText());			
			
			message.setText(str.toString());
			mailSender.send(message);
			comment.setMailSent(true);
			commentDao.save(comment);
		}
	}
}
